package com.snopy.logistics.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;

/**
 * @author snopy
 * @version 1.0.0
 * @classname MonitorController
 * @description
 *
 * @email 77912204@qq.com
 * @date 2022/06/22 22:17
 */
@Slf4j
@RestController
@RequestMapping("monitor")
public class MonitorController {
    //ramp-up period
    private Thread thread;
    private Thread thread2;
    @PostMapping("/start")
    public void start(){
        thread = new Thread(()->{
           while (true){

                   log.info("monitor................");
                   Properties properties = System.getProperties();
                   properties.forEach((key, value) -> log.info("key:{},value:{}", key, value));
                   boolean flag = Thread.currentThread().isInterrupted();
                   if (flag){
                       log.info("等待任务结束---------");
                       break;
                   }
                   try {
                       TimeUnit.SECONDS.sleep(1);
                   } catch (InterruptedException e) {
                       e.printStackTrace();
                       log.info("再次结束·········");
                       Thread.currentThread().interrupt();
                   }
           }

        });
        thread.start();
    }
    @PostMapping("/stop")
    public void stop(){
        if (thread!=null){
            thread.interrupt();
        }
    }
    @PostMapping("/startbark")
    public void startbark(){
        thread2 = new Thread(()->{
            while (true){

                log.info("monitor................");
                Properties properties = System.getProperties();
                properties.forEach((key, value) -> log.info("key:{},value:{}", key, value));
                boolean flag = Thread.currentThread().isInterrupted();
                if (flag){
                    log.info("等待任务结束---------");
                    break;
                }
                LockSupport.park(this);
            }
        });
        thread2.start();
    }

    @PostMapping("/unpark")
    public void unpark(){
        LockSupport.unpark(thread2);
    }
}
